package com.shop.operator.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.shop.domain.CustomGoods;
import com.shop.domain.OperatorQueryGoodsVo;
import com.shop.domain.PageResult;
import com.shop.mapper.BrandMapper;
import com.shop.mapper.GoodsMapper;
import com.shop.mapper.Goods_descMapper;
import com.shop.mapper.ItemMapper;
import com.shop.model.Brand;
import com.shop.model.Goods;
import com.shop.model.GoodsExample;
import com.shop.model.GoodsExample.Criteria;
import com.shop.model.Goods_desc;
import com.shop.model.Item;
import com.shop.model.ItemExample;
import com.shop.operator.service.OperatorGoodsService;
@Service("operatorGoodsService")
public class OperatorGoodsServiceBean implements OperatorGoodsService {
	@Resource
	private GoodsMapper goodsMapper;//spu基本信息
	@Resource
	private Goods_descMapper goods_descMapper;//spu详细信息
	@Resource
	private ItemMapper itemMapper;//sku详细信息
	@Resource
	private BrandMapper brandMapper;//品牌信息
	
	@Override
	public List<Goods> findAll() throws Exception{
		GoodsExample example=new GoodsExample();
		Criteria criteria=example.createCriteria();
		criteria.andIsDeleteIsNull();//标记商品Isdelete字段为空
		criteria.andAuditStatusEqualTo("0");//状态处于未审核
		List<Goods> goodsList=this.goodsMapper.selectByExample(example);
		return goodsList;
	}
	
	@Override
	public PageResult findPage(int pageNum, int pageSize) throws Exception {
		PageHelper.startPage(pageNum, pageSize);//mybities分页插件
		GoodsExample example=new GoodsExample();
		Criteria criteria=example.createCriteria();
		criteria.andIsDeleteIsNull();//标记商品Isdelete字段为空
		criteria.andAuditStatusEqualTo("0");//状态处于未审核
		Page<Goods> page=(Page<Goods>) this.goodsMapper.selectByExample(example); 
		return new PageResult(page.getTotal(),page.getResult());
	}
	
	@Override
	public void updateGoodsStatus(String ids, String status) throws Exception {
		String[] id_string=ids.split(",");
		for(String id:id_string){
			//System.out.println(id);
			Goods goods=this.goodsMapper.selectByPrimaryKey(Long.parseLong(id));
			goods.setAuditStatus(status);
			this.goodsMapper.updateByPrimaryKey(goods);
		}
	}

	@Override
	public void deleteGoods(String ids) throws Exception {
		String[] id_string=ids.split(",");
		for(String id:id_string){
			//System.out.println(id);
			Goods goods=this.goodsMapper.selectByPrimaryKey(Long.parseLong(id));
			goods.setIsDelete("1");//表示逻辑删除
			this.goodsMapper.updateByPrimaryKey(goods);
		}
	}

	@Override
	public PageResult findPageByName(CustomGoods goods,int pageNum,int  pageSize) throws Exception {
		PageHelper.startPage(pageNum, pageSize);//mybities分页插件
		GoodsExample example=new GoodsExample();
		Criteria criteria=example.createCriteria();
		criteria.andIsDeleteIsNull();//标记商品Isdelete字段为空
		criteria.andAuditStatusEqualTo("0");//状态处于未审核
		if(goods!=null){//商品不为空
			String goodsName=goods.getGoodsName();
			//System.out.println(goodsName);
			if(goodsName!=null&&goodsName.length()>0){//商品名不为空和空串
				criteria.andGoodsNameLike("%"+goodsName+"%");//查询条件（模糊查询）
			}
		}
		Page<Goods> page=(Page<Goods>) this.goodsMapper.selectByExample(example); 
		return new PageResult(page.getTotal(),page.getResult());
	}

	

	@Override
	public OperatorQueryGoodsVo findGoodsDetailsByid(String id) throws Exception {
		OperatorQueryGoodsVo operatorQueryGoodsVo=new OperatorQueryGoodsVo();//商品审核详情包装类
		Long goodsId=Long.parseLong(id);//商品id 
		Goods goods=this.goodsMapper.selectByPrimaryKey(goodsId);//商品基本信息spu
		Goods_desc goods_desc=this.goods_descMapper.selectByPrimaryKey(goodsId);//查找商品详细信息spu
		ItemExample example=new ItemExample();
		com.shop.model.ItemExample.Criteria criteria=example.createCriteria();//使用正则查找信息
		criteria.andGoodsIdEqualTo(goodsId);//根据商家id
		List<Item> itemList=this.itemMapper.selectByExample(example);//查找规格商品sku表
		Brand goodsBrand=this.brandMapper.selectByPrimaryKey(goods.getBrandId());//根据商品表查出的品牌id查找品牌
		operatorQueryGoodsVo.setGoods(goods);
		operatorQueryGoodsVo.setGoods_desc(goods_desc);
		operatorQueryGoodsVo.setItemList(itemList);
		operatorQueryGoodsVo.setGoodsBrand(goodsBrand);
		return operatorQueryGoodsVo;
	}

}
